1472F - New Year's Puzzle - CodeForces Solution


brute force dp graph matchings greedy sortings *2100

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h> 
using namespace std;
#define ll                long long
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define nl                "\n"
#define all(x)            (x).begin(),(x).end()
#define sz(x)             (int)((x).size())
#define pii               pair<int, int>
#define pll               pair<long long, long long>
#define pdd               pair<double, double>
#define mp(x, y)          make_pair(x, y)
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define lb(vec, item)      lower_bound((vec).begin(), (vec).end(), item)
#define ub(vec, item)      upper_bound((vec).begin(), (vec).end(), item)

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
 
const long long INF=1e18;
const int32_t M=1e9+7;
const int32_t MM=998244353;

void run_case(){
    int n, m;
    cin >> n >> m;
    
    vector<pii> a(m);
    
    for(auto &x: a)
        cin >> x.first >> x.second;
    
    sort(all(a), [&](pii one, pii two) {
        return one.second < two.second;
    });
    
    vector<pii> v;
    
    auto check = [&]() {
        if(sz(v) & 1)
            return false;
        
        for(int i = sz(v) - 1; i >= 0; i -= 2) {
            int diff = abs(v[i].second - v[i - 1].second - 1);
            if(abs(v[i].first - v[i - 1].first) != diff % 2)
                return false;
        }  
        
        return true;
    };
    
    bool ans = true;
    
    for(int i = 0; i < m; ++i) {
        if(!v.empty() and a[i].second == v.back().second) {
            v.ppb();
            ans &= check();
            v.clear();
        } else {
            v.pb(a[i]);
        }
    }
    
    ans &= check();
    
    cout << (ans ? "YES\n" : "NO\n");
}
 
int main(){

#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    ios_base::sync_with_stdio(false); 
    cin.tie(0); cout.tie(0);
    
    int tests;
    cin >> tests;
    while(tests--){
        run_case();
    }

}


Comments

Submit
0 Comments
More Questions

2B - The least round way
1324A - Yet Another Tetris Problem
246B - Increase and Decrease
22E - Scheme
1566A - Median Maximization
1278A - Shuffle Hashing
1666F - Fancy Stack
1354A - Alarm Clock
1543B - Customising the Track
1337A - Ichihime and Triangle
1366A - Shovels and Swords
919A - Supermarket
630C - Lucky Numbers
1208B - Uniqueness
1384A - Common Prefixes
371A - K-Periodic Array
1542A - Odd Set
1567B - MEXor Mixup
669A - Little Artem and Presents
691B - s-palindrome
851A - Arpa and a research in Mexican wave
811A - Vladik and Courtesy
1006B - Polycarp's Practice
1422A - Fence
21D - Traveling Graph
1559B - Mocha and Red and Blue
1579C - Ticks
268B - Buttons
898A - Rounding
1372B - Omkar and Last Class of Math